<?php

namespace App\Models;

use App\Models\Traits\CategoryCacheTrait;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use App\Admin\Extensions\Traits\ModelTree;
use Illuminate\Database\Eloquent\Factories\HasFactory;

/**
 * App\Models\Category
 *
 * @property int $id
 * @property int $parent_id
 * @property int $transfer_id
 * @property string|null $name
 * @property string|null $name_mini abbreviation
 * @property string|null $image category image
 * @property int $order
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection|Category[] $children
 * @property-read int|null $children_count
 * @property-read Category $parent
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Product[] $products
 * @property-read int|null $products_count
 * @method static \Database\Factories\CategoryFactory factory(...$parameters)
 * @method static \Illuminate\Database\Eloquent\Builder|Category newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Category newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Category query()
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereImage($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereNameMini($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereOrder($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereParentId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereTransferId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property int $is_show
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereIsShow($value)
 * @method static Builder|Category show()
 */
class Category extends Model
{
    use HasFactory;
    use ModelTree;
    use CategoryCacheTrait;
	
	protected $guarded = ['id'];
	
	protected $casts = [
		'is_show' => 'boolean',
	];
	
	public function __construct(array $attributes = [])
	{
		parent::__construct($attributes);
		
		$this->setParentColumn('parent_id');
		$this->setOrderColumn('order');
		$this->setTitleColumn('name');
	}
	
	public function scopeShow(Builder $builder)
	{
		return $builder->where('is_show', true);
	}
	
	public static function isParent(int $parentId)
	{
		return self::find($parentId)->children()->exists();
	}
	
	public function hasChild()
	{
		return $this->children()->exists();
	}
	
	public function hasProduct()
	{
		return $this->products()->exists();
	}
	
	public function parent()
	{
		return $this->belongsTo(Category::class, 'parent_id', 'id');
	}
	
	public function children()
	{
		return $this->hasMany(Category::class, 'parent_id', 'id');
	}
	
	public function products()
	{
		return $this->hasMany(Product::class);
	}
}
